package com.ws.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.ws.commonutils.R;
import com.ws.service.VideoService;
import com.ws.servicebase.exceptionhandler.MyException;
import com.ws.utils.AliyunVodSDKUtils;
import com.ws.utils.ConstantPropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author ws
 * @create 2024-12-09 16:16
 */
@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin
public class VideoController {

    @Autowired
    private VideoService videoService;

    @PostMapping("upload")
    public R getVideoId(MultipartFile file) {
        String videoId = videoService.getVideoId(file);
        System.out.println(videoId);
        return R.ok().data("videoId", videoId);
    }

    @DeleteMapping("deleteVod/{id}")
    public R deleteVideo(@PathVariable("id") String id) {
        try {
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest();
            deleteVideoRequest.setVideoIds(id);
            client.getAcsResponse(deleteVideoRequest);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(20001,"删除视频失败");
        }


    }
    @DeleteMapping("delete-batch")
    public R deleteBatch(@RequestParam("videoIdList") List<String> videoIdList) {
        videoService.deleteBatch(videoIdList);
        return R.ok();
    }
    @GetMapping("getVideoPlayAuth/{videoId}")
    public R getVideoPlayAuth(@PathVariable("videoId") String videoId) {
        try {
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(videoId);
            GetVideoPlayAuthResponse acsResponse = client.getAcsResponse(request);
            String playAuth = acsResponse.getPlayAuth();
            return R.ok().message("获取凭证成功").data("playAuth", playAuth);
        }catch (Exception e) {
            throw new MyException(20001,"获取凭证失败");
        }
    }

}
